mysql中alter语句中change和modify的区别

您所在的位置:网站首页 mysql modify字段 mysql中alter语句中change和modify的区别

mysql中alter语句中change和modify的区别

2023-03-22 12:11| 来源: 网络整理| 查看: 265

mysql中alter语句中change和modify的区别为

当需要修改字段名称时使用change;当需要修改字段类型时使用modify

change和modify的具体使用场景如下

CHANGE子句对列进行重命名。重命名时,需给定旧的和新的列名称和列当前的类型。要把一个INTEGER列的名称从column_a变更到column_b:

ALTER TABLE t1 CHANGE column_a column_b INTEGER

如果想要更改列的类型而不是名称, CHANGE语法仍然要求旧的和新的列名称,即使旧的和新的列名称是一样的。例如:

ALTER TABLE t1 CHANGE column_b column_b BIGINT NOT NULL

使用MODIFY来改变列的类型,此时不需要重命名:

ALTER TABLE t1 MODIFY column_b BIGINT NOT NULLmodify能修改字段类型和约束,而change不能。

change用来字段重命名,不能修改字段类型和约束;

modify不用来字段重命名,只能修改字段类型和约束;

试验比较:

1、字段重命名:

1)change

mysqlalter table t1 change number id char(2)

Query OK, 0 rows affected (0.08 sec)

Records: 0 Duplicates: 0 Warnings: 0

2)modify

mysqlalter table t1 modify id num int(2)

ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'num int(2)' at line 1

mysql

结论:能用change重命名,而modify不能。

2、修改字段类型和约束

1)modify

mysqlalter table t1 modify id int(2)

Query OK, 0 rows affected (0.06 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysqlalter table t1 modify id int(2) not null

Query OK, 0 rows affected (0.08 sec)

Records: 0 Duplicates: 0 Warnings: 0

2)change

mysqlalter table t1 change id char(2)

ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'char(2)' at line 1

mysqlalter table t1 change id char(2) not null

ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'char(2) not null' at line 1

结论:modify能修改字段类型和约束,而change不能。

最终结论:change用来字段重命名,不能修改字段类型和约束;

modify不用来字段重命名,只能修改字段类型和约束;

欢迎分享,转载请注明来源:内存溢出

原文地址:https://outofmemory.cn/zaji/6132656.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3